package com.lwb.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwb.commonutils.JwtUtils;
import com.lwb.commonutils.Row;
import com.lwb.commonutils.UcenterMemberVo;
import com.lwb.eduservice.client.UcenterMemberClient;
import com.lwb.eduservice.entity.EduComment;
import com.lwb.eduservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/eduservice/edu-comment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    //分页查询
    @PostMapping("findAllComment/{page}/{limit}/{courseId}")
    public Row findAllComment(@PathVariable long page,@PathVariable long limit,@PathVariable String courseId){
        Page<EduComment> pageInfo = new Page<>(page,limit);
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        eduCommentService.page(pageInfo,queryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("items",pageInfo.getRecords());
        map.put("total",pageInfo.getTotal());
        map.put("current",pageInfo.getCurrent());
        map.put("size",pageInfo.getSize());
        map.put("pages",pageInfo.getPages());
        map.put("hasNext",pageInfo.hasNext());
        map.put("hasPrevious",pageInfo.hasPrevious());
        return Row.SUCCESS().data(map);
    }

    //添加评论
    @PostMapping("addComment")
    public Row addComment(@RequestBody EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return Row.ERROR().message("请先登录！！");
        }
        UcenterMemberVo userInfo = ucenterMemberClient.getUserInfo(memberId);
        comment.setAvatar(userInfo.getMemberAvatar());
        comment.setMemberId(userInfo.getMemberId());
        comment.setNickname(userInfo.getNickname());

        eduCommentService.save(comment);
        return Row.SUCCESS();
    }

}

